local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.Server_PlayerComponent(),script.Parent.Name);
local function_list = _G.ServerOHOH.function_list
local trail_cfg = ServerOHOH.config.Trail
local ItemTool=_G.CommonOHOH.load.ItemTool()
local MarketplaceService = game:GetService("MarketplaceService")
function M.new(self,...)
	return M(self,...);
end;



function M.ctor(self, parent)
	M.super.ctor(self, parent);	
    self.player=self.parent
	self.DataBase = self.player:GetComponent(ServerOHOH.load.Server_PlayerDataComponent())

	self.player.main_inst.CharacterAdded:Connect(function(character)
		print(self.player.character)
		self:InitEquipPosition()
    end)
end


function M.EquipTrail(self,name,trailEffect)
	local equipName = nil
	if typeof(name) == "number" then
		equipName = tostring(name)
	else
		equipName = name
	end
	print("Equipname:",equipName)
	if self.DataBase:GetData("trail").had[equipName] then
		self.DataBase:GetData("trail").equip = equipName
		self:GenerateTrail(trailEffect)
	else
		return false
	end
	self.DataBase:Execut(function()
    end);
    self.player:SynchDataToClient("trail")
    return true
end

function M.BuyTrail(self,name,id)
	if self:IsHad(name) then
		--判断购买类型分别处理
		if self:Buy(id,name) then
			return true
		else
			return false
		end
	end
end

function M.IsHad(self,name)
	if self.DataBase:GetData("trail").had[name] == nil then
		return true
	end
	return false
end

function M.Buy(self,id,name)
	local Name = nil
	if typeof(name) == "number" then
		Name = tostring(name)
	end

	local costId = trail_cfg[id].costId
	local costCount = trail_cfg[id].price

	if ItemTool.IsRubux(costId) then
		print("robux购买")
		MarketplaceService:PromptPurchase(self.parent.main_inst,trail_cfg[id].pid)
		return false
	else
		if ItemTool.IsHadEnough(costId,costCount,self.DataBase) then
			local key,id = ItemTool.GetKeyAndID(costId)
			self.DataBase:GetData(key)[id] = self.DataBase:GetData(key)[id]-costCount
			self.DataBase:Execut(function()
			end);
			self.player:SynchDataToClient("base")
		else
			print("wins不足")
			return false
		end
	end

	self.DataBase:GetData("trail").had[Name] = 1
	self.DataBase:Execut(function()
	end);
	self.player:SynchDataToClient("trail")
	return true
end

function M.IsEnough(self,costId,needCount)
	local count = ItemTool.GetItemCount(costId,self.DataBase)
	if needCount>count then
		return false
	else
		return true
	end
end

--给每个玩家生成两个
function M.InitEquipPosition(self)
	self.player.attchment0 = Instance.new("Attachment")
	self.player.attchment1 = Instance.new("Attachment")
	self.player.attchment0.Parent = self.player.character:WaitForChild("LeftUpperArm")
	self.player.attchment1.Parent = self.player.character:WaitForChild("RightUpperArm")
end

function M.GenerateTrail(self,trailEffect)
	if self.player.trail then
		self.player.trail:Destroy()
	end
	self.player.trail = trailEffect:Clone()
	self.player.trail.Parent = self.player.character.root_part
	self.player.trail.Name = "trail"
	self.player.trail.Attachment0 = self.player.attchment0
	self.player.trail.Attachment1 = self.player.attchment1
end



function M.dtor(self)
	M.super.dtor(self);
end

M:RegistFunction(function_list.equip_trail,M.EquipTrail)

M:RegistFunction(function_list.buy_trail,M.BuyTrail)

return M